<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Review_model extends MY_Model
{

	public function reviewList($data = array(),$per_page,$limit){
        $where = " 1=1 ";
        if(!empty($data['starttime'])){
            $where .= " AND createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND createtime <= {$data['endtime']}";
        }
        if(!empty($data['newsid'])){
            $where .= " AND newsID = {$data['newsid']}";
        }
        if(!empty($data['content'])){
            $where .= " AND content like '%{$data['content']}%'";
        }
        if($data['status'] === '0' || in_array($data['status'],[-1,1,2])){
            $where .= " AND status={$data['status']}";
        }
        $limit = " limit {$per_page},{$limit}";
        $sql  = "SELECT * FROM blues_news_review WHERE  {$where} ORDER BY createtime desc {$limit}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    public function get_review_cnt($data = array()){
        $where = " 1=1 ";
        if(!empty($data['starttime'])){
            $where .= " AND createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND createtime <= {$data['endtime']}";
        }
        if(!empty($data['content'])){
            $where .= " AND content like '%{$data['content']}%'";
        }
        if($data['status'] === '0' || in_array($data['status'],[-1,1,2])){
            $where .= " AND status={$data['status']}";
        }
        
        $sql  = "SELECT * FROM blues_news_review WHERE  {$where} ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }


    //编辑状态
    public function edit_status($id,$status){
        $this->db->where("reviewId",$id);
        if($status == -1)
        {
            $res = $this->db->delete("blues_news_review");
            return $res;
        }
        $data = array(
            'status'=>$status
        );
        $res = $this->db->update("blues_news_review",$data);
        return $res;
    }

    public function getReviewCntByID($newsID)
    {
        $sql = "SELECT count(*) cnt FROM blues_news_review WHERE newsID={$newsID} and status =1";
        $query = $this->db->query($sql);
        return $query->row_array['cnt'];
    }

    public function getReviewInfoByID($reviewId)
    {
        $this->db->where("reviewId",$reviewId);
        $query = $this->db->get("blues_news_review");
        return $query->row_array();
    }
    public function updateReviewCnt($newsID)
    {
        $selectsql = "SELECT count(*) cnt FROM blues_news_review WHERE newsID={$newsID} and status =1";
        $query = $this->db->query($selectsql);
        $reviewCnt = $query->row_array()['cnt'];
        $res = 1;
        if($reviewCnt)
        {
            $sql = "UPDATE blues_news SET reviewcnt = {$reviewCnt} WHERE newsID = {$newsID}";
            $res = $this->db->query($sql);    
        }
        return $res;
    }


}


